using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace Microsoft.VSPowerToys.PackInstaller
{
    public partial class PackInstallerLicenseAgreementForm : Form
    {
        private int checkPrint;

        /// <summary>
        /// Constructor, sets the fonts we want to use.
        /// </summary>
        public PackInstallerLicenseAgreementForm()
        {
            InitializeComponent();

            this.Font = SystemFonts.DialogFont;
            this.licenseAgreementTextBox.Font = SystemFonts.CaptionFont;
        }

        /// <summary>
        /// In the load event we want to load the license agreement contents and make sure the decline button is selected.
        /// </summary>
        private void PackInstallerLicenseAgreementForm_Load(object sender, EventArgs e)
        {
            this.licenseAgreementTextBox.Rtf = Properties.Resources.Pack_Installer_License_Agreement;
            this.declineButton.Select();
        }

        /// <summary>
        /// When the user clicks "I Accept" we want to record which version has been accepted in a user setting and save it to disk.
        /// </summary>
        private void acceptButton_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.LastLicenseAccepted = Application.ProductVersion;
            Properties.Settings.Default.Save();
            this.DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// This method will check to see if the user accepted the license for this version of software.
        /// </summary>
        static public bool HasCurrentLicenseVersionBeenAccepted()
        {
            string lastAccepted =  Properties.Settings.Default.LastLicenseAccepted;
            return (string.Equals(Application.ProductVersion, lastAccepted));
        }

#if DEBUG
        /// <summary>
        /// Reset all settings to default
        /// </summary>
        static public void ResetLastLicenseAcceptedSetting()
        {
            Properties.Settings.Default.Reset();
        }
#endif


        /// <summary>
        /// Trigger printing...
        /// </summary>
        private void printLicenseButton_Click(object sender, EventArgs e)
        {
            licensePrintDialog.Document = licenseDocument;
            if (licensePrintDialog.ShowDialog() == DialogResult.OK)
            {
                licenseDocument.Print();
            }
        }

        /// <summary>
        /// The PrintPage event is sent each time a page needs to be printed.
        /// This code is lifted mostly from the MSDN article about using the PrintDocument object.
        /// <param name="sender"
        /// <param name="e"></param>
        /// </summary>
        private void licenseDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            // Print the content of RichTextBox. Store the last character printed.
            checkPrint = licenseAgreementTextBox.Print(checkPrint, licenseAgreementTextBox.TextLength, e);

            // Check for more pages
            if (checkPrint < licenseAgreementTextBox.TextLength)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
        }

        /// <summary>
        /// The BeginPrint event is sent when printing begins for the document.
        /// This code resets the counter used by the PrintPage event to print the RichTextBox
        /// <param name="sender"
        /// <param name="e"></param>
        /// </summary>
        private void licenseDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            checkPrint = 0;
        }
    }
}